package jmcnet.libcommun.containers.carousel
{
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.BlurFilter;
import flash.geom.Point;

import mx.core.UIComponent;
import mx.events.FlexEvent;

public class Carousel extends UIComponent
{
	public var logos:Array; 
	import flash.display.Stage;
	public var numOfObj:uint;
	public var radius:Point;
	public var center:Point;
	public var speed:Number=0.0;
	public var sortedItems:Array = new Array();
	public var item:CarouselItem;
	public var w:Number;
	public var h:Number;
	public var padding:Number;
	public var distance:Number;
	public var vitesse:Number;
	public var fallof:Number;
	
	private var started:Boolean = false;
	
	public function Carousel() {
		trace("Construction Carousel : w="+w+" h="+h+" logos="+logos+" fallof="+fallof+" padding="+padding+" distance="+distance+" vitesse="+vitesse);
		addEventListener(FlexEvent.CREATION_COMPLETE, init);
		addEventListener(FlexEvent.SHOW,start);
		addEventListener(FlexEvent.HIDE,stop);
		// JMC le 03/09/2011 -> fait drole lorsque l'on clique en dehors de la fenetre
		// addEventListener(Event.DEACTIVATE,stop);
		// addEventListener(Event.ACTIVATE,start);
	}
	
	/* public function Carousel(w:Number, h:Number, logos:Array, fallof:Number, padding:Number, distance:Number, vitesse:Number) {
		init(w, h, logos, fallof, padding, distance, vitesse);
	}
	*/
	
	public function init(event:FlexEvent):void {
		trace("Carousel.init : w="+w+" h="+h+" logos="+logos+" fallof="+fallof+" padding="+padding+" distance="+distance+" vitesse="+vitesse);

		this.numOfObj = this.logos.length;
		
		center=new Point(w/2-this.padding,h/2-this.padding);
		radius = new Point(w/2-this.padding,this.padding+(this.padding/2));
		
		for(var i:int=0;i<numOfObj;i++){		
			item = new CarouselItem(logos[i]);
			item._falloff = fallof;
			item._angle=(i*((Math.PI*2)/numOfObj));
			sortedItems.push(item);			
			addChild(item);
		}
		start();
	}
	
	public function start(event:Event = null):void {
		trace("Carousel.start");
		if (started) {
			trace("Carousel.start : déjà actif");
			return;
		}
		var item:CarouselItem;
		for each (item in sortedItems) {
			item.removeEventListener(Event.ENTER_FRAME,onEnter);
			item.addEventListener(Event.ENTER_FRAME,onEnter);
			item.removeEventListener(MouseEvent.CLICK,itemClick);
			item.addEventListener(MouseEvent.CLICK,itemClick);
		}
		started = true;
	}
	
	public function stop(event:Event = null):void {
		trace("Carousel.stop");
		if (!started) {
			trace("Carousel.stop : déja stoppé");
			return;
		}
		var item:CarouselItem;
		for each (item in sortedItems) {
			item.removeEventListener(Event.ENTER_FRAME,onEnter);
			item.removeEventListener(MouseEvent.CLICK,itemClick);
		}
		started = false;
	}
	 
	public function arrange():void {
	    sortedItems.sortOn("y", Array.NUMERIC);
	    var i:int = sortedItems.length;
	    while(i--){
	        if (getChildAt(i) != sortedItems[i]) {
	            setChildIndex(sortedItems[i], i);
	        }
	    }
	}
	
	public function onEnter(evt:Event):void {
		var obj:CarouselItem= evt.currentTarget as CarouselItem;	
		obj.y=Math.sin(obj._angle) *radius.y +center.y;
		var scale:Number=obj.y /(center.y+radius.y);
		obj.scaleX=obj.scaleY=scale*1;
		obj.x=((Math.cos(obj._angle) *radius.x) +center.x)+((30/(this.h/400))/2);
		
		var blur:BlurFilter = new BlurFilter(1/(scale*scale),1/(scale*scale),1);
		obj.filters = [blur];
		obj._angle=(obj._angle+this.speed);
		obj.distance = distance;
		arrange();
		obj.Wave();
	}
	
	/*
	 * lorsque l'on clique sur un item
	 */
	public function itemClick(evt:MouseEvent):void {
		trace("Carousel.itemClick");
		var item:CarouselItem = evt.currentTarget as CarouselItem;
		if(item.y > (this.h/2)) {
			// evt.currentTarget.isWave = true
		};
	}
	
	public function onMove(evt:MouseEvent):void {
		trace("Carousel:onMove mouseX="+this.mouseX);
		this.speed = (this.mouseX - center.x-(this.padding/2))/this.vitesse;
		trace("Carousel:onMove speed="+speed);
	}

}
}